package com.jiyun.controller;

import com.jiyun.pojo.City;
import com.jiyun.pojo.Dept;
import com.jiyun.pojo.Emp;
import com.jiyun.service.EmpService;
import com.jiyun.pojo.EmpVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class EmpController {

    @Autowired
    EmpService empService;


    @RequestMapping("findAll")
    public String findAll(HttpServletRequest request){
        List<EmpVo> all = empService.findAll();
        request.setAttribute("empList",all);
        return "emp";
    }

    @RequestMapping("toAdd")
    public String toAdd(Model Model){
        List<Dept> deptList=empService.deptList();
        List<City> cityList=empService.cityList();
        Model.addAttribute("dlist",deptList);
        Model.addAttribute("clist",cityList);
        return "add";
    }
    @RequestMapping("add")
    public  String add(Emp emp){
        empService.add(emp);
        return "redirect:findAll";
    }
    @RequestMapping("delEmp")
    public  String delEmp(Integer eid){
        empService.delEmp(eid);
        return "redirect:findAll";
    }

    @RequestMapping("toUpdate")
    public String toUpdate(Model Model){
        List<Dept> deptList=empService.deptList();
        List<City> cityList=empService.cityList();
        Model.addAttribute("dlist",deptList);
        Model.addAttribute("clist",cityList);
        return "update";
    }
    @RequestMapping("update")
    public  String updateEmp(Emp emp){
        empService.updateEmp(emp);
        return "redirect:findAll";
    }


}
